package com.huyandroid.AccountMonitor1;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class UsersHandler extends SQLiteOpenHelper{
	///ALL STATIC VARIABLES for 1 DATABASE --CHANGED DEPEND EACH APPLICATION
		//Database:
		private static final int DATABASE_VERSION = 1;
		private static final String DATABASE_NAME = "usersManager";
		//Table + Column names
		private static final String TABLE_DATA = "users";
		private static final String KEY_ID = "id";   //Column 0
		private static final String KEY_COL1 = "user_name"; //Column 1
		private static final String KEY_COL2 = "password"; //Column 2
		
		private static final String KEY_COL3= "full_name";  //Column 3
		private static final String KEY_COL4="email"; //Column 4
		private static final String KEY_COL5="phone"; //Column 5
		private static final String KEY_COL6="account" ; //Column 6
		
		String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_DATA + "("
				+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_COL1 + " TEXT,"
				+ KEY_COL2 + " TEXT,"+ KEY_COL3 + " TEXT,"+ KEY_COL4 + " TEXT,"+ KEY_COL5 + " TEXT,"
				+ KEY_COL6 + " TEXT"+");";
		///Database Variables
		private Context mContext;
		private SQLiteDatabase mDB;
		////Constructors//////////////////////////////////////////////
		public UsersHandler(Context c)
		{
			super(c, DATABASE_NAME, null, DATABASE_VERSION);
			mContext =c;
		}
		
		public UsersHandler(Context c,String DBname)
		{
			super(c, DBname, null, DATABASE_VERSION);
			mContext =c;
		}
		///////////////////////////////////////////////////////////
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_USERS_TABLE);
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATA);

			// Create tables again
			onCreate(db);
		}
		//////////////////OTHER STANDARD OPERATIONS////////////////////////
		public UsersHandler open(){
			//super(mContext, DATABASE_NAME, null, DATABASE_VERSION);
			mDB=this.getWritableDatabase();
			return this;
		}
		public void close()
		{
			mDB.close();
		}
		public long addItem(User u){
			ContentValues v = new ContentValues();
			v.put(KEY_COL1, u.getUserName());
			v.put(KEY_COL2, u.getPassword());
			//v.put(KEY_COL3, c.getEmail());
			//insert table
			return mDB.insert(TABLE_DATA, null, v);
		}
		
		public long addAll2Item(User u){
			ContentValues v = new ContentValues();
			Information i=u.getInformation();
			v.put(KEY_COL1, u.getUserName());
			v.put(KEY_COL2, u.getPassword());
			
			v.put(KEY_COL3,i.getName());
			v.put(KEY_COL4,i.getEmail());
			v.put(KEY_COL5,i.getPhoneNumber());
			v.put(KEY_COL6,i.getAccount());
			return mDB.insert(TABLE_DATA, null, v);
			
		}
		public List<User> getAllItems(){
			List<User> L = new ArrayList<User>();
			String selectQuery = "SELECT  * FROM " + TABLE_DATA;
			Cursor c = mDB.rawQuery(selectQuery, null);
			if(c.moveToFirst()){
				do{
					User u=new User();
					u.setId(Integer.parseInt(c.getString(0))); //Id of the table != _ID in the object User
					u.setUserName(c.getString(1));//user name
					u.setPassword(c.getString(2));//password
					L.add(u);
				}while (c.moveToNext());
			}
			return L;
		}
		public Cursor getItem(long rowId){
			Cursor c=mDB.query(true, TABLE_DATA, new String[]{KEY_COL1,KEY_COL2}, KEY_ID+"="+rowId,
					null, null, null, null, null);
			if(c!=null) c.moveToFirst();
			return c;
			
		}
		
		////get all information form 1 user
		public String[] getAllfromItem(long rowId){
			Cursor c=mDB.query(true, TABLE_DATA, new String[]{KEY_ID,KEY_COL1,KEY_COL2,KEY_COL3,KEY_COL4,KEY_COL5,KEY_COL6}, 
					KEY_ID+"="+rowId,null, null, null, null, null);
			if(c!=null) c.moveToFirst();
			String[] temp=new String[7];
			for(int i=0;i<7;i++) temp[i]=c.getString(i);
			return temp;
			}

		public boolean deleteItem(long rowId){
			return mDB.delete(TABLE_DATA, KEY_ID + "=" + rowId, null) >0;
		}
		public boolean updateItem(long rowId,User u){
			ContentValues v = new ContentValues();
			Information i=u.getInformation();
			v.put(KEY_COL1, u.getUserName());
			v.put(KEY_COL2, u.getPassword());
			
			v.put(KEY_COL3,i.getName());
			v.put(KEY_COL4,i.getEmail());
			v.put(KEY_COL5,i.getPhoneNumber());
			v.put(KEY_COL6,i.getAccount());
			return mDB.update(TABLE_DATA, v, KEY_ID+"="+rowId,null)>0;
		}
		public boolean updateItem(long rowId,String name,String email,String phone,String account){
			ContentValues v = new ContentValues();
			
			v.put(KEY_COL3,name);
			v.put(KEY_COL4,email);
			v.put(KEY_COL5,phone);
			v.put(KEY_COL6,account);
			return mDB.update(TABLE_DATA, v, KEY_ID+"="+rowId,null)>0;
		}
}
